package com.keelau.ui.view;

import javafx.collections.ObservableList;
import javafx.scene.control.TreeItem;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

public class AliCataLogTreeItem extends TreeItem<AliCatalogTree> {


    public boolean isFirsetChildren = true;

    Logger logger = LoggerFactory.getLogger(AliCataLogTreeItem.class);

    public AliCataLogTreeItem(AliCatalogTree value){
        super(value);
    }
    private boolean isFirstTimeChildren = true;

    @Override
    public ObservableList<TreeItem<AliCatalogTree>> getChildren() {
        //return super.getChildrens();

        if (isFirstTimeChildren) {
            isFirstTimeChildren = false;
            List<AliCatalogTree> aliCatalogs = getValue().getChildrens();
            if (!getValue().isLeaf() && aliCatalogs!=null) {
                //logger.info(getValue().getName() + " getChildren:" );
                //把AliCatalogTree 转化为TreeItem.
                List<AliCataLogTreeItem> childNodes = new ArrayList<AliCataLogTreeItem>();
                aliCatalogs.forEach(t -> {
                    childNodes.add(new AliCataLogTreeItem(t));
                });
                super.getChildren().addAll(childNodes);
            }
        }
        return super.getChildren();
    }

    @Override
    public boolean isLeaf() {
        //logger.info(getValue().getName() + " isLeaf:" + getValue().isLeaf());
        return getValue().isLeaf();
    }


}
